import {createRouter, createWebHashHistory} from 'vue-router'
import type {RouteRecordRaw} from "vue-router"
// import apiClient from '@/api/api';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/userLogin',
    }
    ,
    {
        path: '/userLogin',
        name: 'userLogin',
        component: () => import('@/views/userLogin.vue')
    }
    ,
    // {
    //     path: '/test02',
    //     name: 'test02',
    //     component: () => import('@/views/test/test02.vue'),
    // },
    // {
    //     path: '/test03',
    //     name: 'test03',
    //     component: () => import('@/views/test/test03.vue'),
    // },
    // {
    //     path: '/test04',
    //     name: 'test04',
    //     component: () => import('@/views/test/test04.vue'),
    // },
    // {
    //     path: '/test05',
    //     name: 'test05',
    //     component: () => import('@/views/test/test05.vue'),
    // },
    // {
    //     path: '/test06',
    //     name: 'test06',
    //     component: () => import('@/views/test/test06.vue'),
    // },
    // {
    //     path: '/map',
    //     name: 'three3DMap',
    //     component: () => import('@/views/test/Three3DMap.vue') // ✅ 必须指向这个组件
    // },
    //
    // {
    //     path: '/threeMap',
    //     name: 'threeMap',
    //     component: () => import('@/views/regionalPublicBrand/threeMap/map/map3D.vue')
    // }
    // ,
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/register.vue')
    }
    ,
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/HomeView.vue'),
        children: [
            {
                path: '/homepage',
                name: 'homepageThree',
                component: () => import('@/views/homepageThree/index.vue'),
            },
            {
                path: '/vepCalcDev',
                name: 'vepCalcDev',
                component: () => import('@/views/vepDev2/vepD4-index.vue'),
            },
            {
                path: '/logout',
                redirect: '/userLogin',
            },
            {
                path: '/gepCalcDev',
                name: 'gepCalcDev',
                component: () => import('@/views/gepDev4/gepD4-index.vue'),
            },

            {
                path: '/ecoProduce',
                name: 'ecoProduce',
                component: () => import('@/views/ecoProduce/ecoProduce-index.vue'),
                children: [
                    {
                        path: 'product01',
                        name: 'ecoProducePage01',
                        component: () => import('@/views/ecoProduce/pages/product01.vue'),
                    },
                    {
                        path: 'product02',
                        name: 'ecoProducePage02',
                        component: () => import('@/views/ecoProduce/pages/product02.vue'),
                    },
                    {
                        path: 'product03',
                        name: 'ecoProducePage03',
                        component: () => import('@/views/ecoProduce/pages/product03.vue'),
                    }
                ]
            },
            {
                path: '/ecoProduce/product01',
                name: 'ecoProducePage01',
                component: () => import('@/views/ecoProduce/pages/product01.vue'),
            },
            {
                path: '/ecoProduce/product02',
                name: 'ecoProducePage02',
                component: () => import('@/views/ecoProduce/pages/product02.vue'),
            },
            {
                path: '/ecoProduce/product03',
                name: 'ecoProducePage03',
                component: () => import('@/views/ecoProduce/pages/product03.vue'),
            },
            {
                path: '/regionalPublicBrand',
                name: 'regionalPublicBrand',
                component: () => import('@/views/regionalPublicBrand/regionalPublicBrand-index.vue'),
                // children: [
                //     {
                //         path: '/regionalPublicBrand/page01',
                //         name: 'regionalPublicBrandPage01',
                //         component: () => import('@/views/regionalPublicBrand/page/brand01.vue'),
                //     },
                // ],
            },
            // {
            //     path: '/regionalPublicBrandTest',
            //     name: 'regionalPublicBrandTest',
            //     component: () => import('@/views/regionalPublicBrand/regionalPublicBrandTest-index.vue'),
            // },
            {
                path: '/regionalPublicBrand/page01',
                name: 'regionalPublicBrandPage01',
                component: () => import('@/views/regionalPublicBrand/page/brand01.vue'),
            },
            {
                path: '/regionalPublicBrand/page02',
                name: 'regionalPublicBrandPage02',
                component: () => import('@/views/regionalPublicBrand/page/brand02.vue'),
            },
            {
                path: '/regionalPublicBrand/page03',
                name: 'regionalPublicBrandPage03',
                component: () => import('@/views/regionalPublicBrand/page/brand03.vue'),
            },
            {
                path: '/regionalPublicBrand/page04',
                name: 'regionalPublicBrandPage04',
                component: () => import('@/views/regionalPublicBrand/page/brand04.vue'),
            },
            {
                path: '/regionalPublicBrand/page05',
                name: 'regionalPublicBrandPage05',
                component: () => import('@/views/regionalPublicBrand/page/brand05.vue'),
            },
            {
                path: '/regionalPublicBrand/page06',
                name: 'regionalPublicBrandPage06',
                component: () => import('@/views/regionalPublicBrand/page/brand06.vue'),
            },
            {
                path: '/regionalPublicBrand/page07',
                name: 'regionalPublicBrandPage07',
                component: () => import('@/views/regionalPublicBrand/page/brand07.vue'),
            },
            {
                path: '/regionalPublicBrand/page08',
                name: 'regionalPublicBrandPage08',
                component: () => import('@/views/regionalPublicBrand/page/brand08.vue'),
            },
            {
                path: '/regionalPublicBrand/page09',
                name: 'regionalPublicBrandPage09',
                component: () => import('@/views/regionalPublicBrand/page/brand09.vue'),
            },
            {
                path: '/regionalPublicBrand/page10',
                name: 'regionalPublicBrandPage10',
                component: () => import('@/views/regionalPublicBrand/page/brand10.vue'),
            },
            {
                path: '/gepCalcDevNan',
                name: 'gepCalcDevNan',
                component: () => import('@/views/gepDev4/gepCalcDevNan.vue'),
            },
            {
                path: '/vepCalcDevNan',
                name: 'vepCalcDevNan',
                component: () => import('@/views/vepDev2/vepCalcDevNan.vue'),
            },
            {
                path: '/product/detail/:name',
                name: 'ProductDetail',
                component: () => import('@/views/ecoProduce/ProductDetail.vue') // 替换为实际组件路径
            },

        ]
    }

]
const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes,
})

// 全局路由守卫 检查token是否过期，过期删除；未登录跳转到登录页面
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token');

    if (token) {
        // 分割 token 获取 payload
        const payload = token.split('.')[1];

        // 解码 Base64Url
        const decodedPayload = JSON.parse(atob(payload));
        // console.log("decodedPayload",decodedPayload)
        const currentTime = Date.now() / 1000;

        // 检查 token 是否过期
        if (decodedPayload.exp < currentTime) {
            console.log("全局路由检查过期，对token进行删除")
            ElMessage.success('请重新登录！');
            localStorage.removeItem('token'); // 删除过期的 token
            next('/userLogin'); // 跳转到登录页面
        } else {
            next(); // 允许访问
        }
    } else {
        // 未登录时跳转到登录页面
        if (to.path !== '/userLogin' && to.path !== '/register') {
            ElMessage.error('请登录！');
            next('/userLogin');
        } else {
            next(); // 放行 '/userLogin' 和 '/register'
        }
    }

});


export default router
